


document.addEventListener("plusready", function() {
                // 注册返回按键事件
                plus.key.addEventListener('backbutton', function() {
                    // 事件处理
                    plus.nativeUI.confirm("退出程序？", function(event) {
                        if (event.index) {
                            plus.runtime.quit();
                        }
                    }, null, ["取消", "确定"]);
                }, false);
            });



    mui.init();
            var _config = {url : 'https://newapp.icandrive.cn/api/'}
            var checkUrl= _config.url + "edition.php";
            //需要传递当前的版本
            function checkUpdate( wgtVer ){


                mui.ajax( checkUrl , { dataType : "json", type : 'POST',
                                      data : {ver : wgtVer},
                                      success : function(json){
                                          plus.nativeUI.closeWaiting();
                                          if( parseInt(json.code) ==1 ){
                                              //可以升级
                                              plus.nativeUI.confirm("检查到当前版本有最新更新，下载升级？",
                                                                    function(event){
                                                  if(event.index ==0){

                                                      downWgt(json.url); //下载更新版的地址
                                                  }
                                              } ,'系统消息',['马上升级','下次再说']);
                                          } else{
//                                              plus.nativeUI.toast("无新版本可更新！");
                                          }
                                      },
                                      error : function(xhr, error){
                                          plus.nativeUI.closeWaiting();
                                          plus.nativeUI.toast('检测更新失败！') ;
                                      }
                                     }) ;
            }
            // 下载wgt文件

            function downWgt(wgtUrl){
                plus.nativeUI.showWaiting("下载更新文件...");
                plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
                    if ( status == 200 ) {
                        //console.log("下载wgt成功："+d.filename);
                        installWgt(d.filename); // 安装wgt包
                    } else {
                        //console.log("下载wgt失败！");
                        plus.nativeUI.alert("下载更新失败！");
                    }
                    plus.nativeUI.closeWaiting();
                }).start();
            }
            // 更新应用资源
            function installWgt(path){
                plus.nativeUI.showWaiting("安装更新文件...");
                plus.runtime.install(path,{},function(){
                    plus.nativeUI.closeWaiting();
                    plus.nativeUI.alert("应用资源更新完成！",function(){
                        plus.runtime.restart();
                    });
                },function(e){
                    plus.nativeUI.closeWaiting();
                    plus.nativeUI.alert("安装更新文件失败["+e.code+"]："+e.message);
                });
            }




  //在页面中初始化plus插件
            mui.init();
            mui.plusReady(function(){
                plus.runtime.getProperty(plus.runtime.appid,function(inf){

                    checkUpdate(inf.version)

                });
            });
            //检测升级
            function  upgrade_app_wgt(){
                plus.runtime.getProperty(plus.runtime.appid,function(inf){
                    checkUpdate( inf.version);
                    console.log(inf.version)
                    sessionStorage.setItem('upgrade','1');
                });
            }


$("#plan").click(function(){
    window.location.href="../plan/plan_1.html";
});
$('.cash').click(function () {
    window.location.href='../cash/cash.html'
});
$(function(){
    var token =localStorage.phone;
    var status =$(".oder_details_active").attr('type');
    $.ajax({
        url:"https://newapp.icandrive.cn/api/teacherorder.php",
        type:'post',
        dataType:'json',
        data:{token:token,status:status},
        success:function(data){
            var now=new Date();
            var hour = now.getHours();
            var minu = now.getMinutes();
            var right_time=hour+minu/60;
            if(data.status=200){
					 $(data.conduct['test']).each(function(){
                    var str = Date.parse(new Date())/1000;

                    	console.log(str);
                    var timestamp = Date.parse(this.order_time)/1000;
                    console.log(timestamp);
                    var date=(str-timestamp)/3600/24;
                    var order_id = this.order_id;
                    var time_id=this.time_id;
                    var user_id=this.user_id;
                    var button='<button onclick="cancel('+order_id+','+2+','+date+','+time_id+','+user_id+')">确认取消</button>';
				console.log(date);
                    if(date>0 && date<1){
                        var a=time(this.time_id);
                        	console.log(a);
                        if(right_time<a[1] && right_time>a[0]){
                            button='<button onclick="accept('+order_id+')">确认接单</button>';
                        }
                        if(right_time>a[1]){
                            button='<button onclick="sub('+order_id+','+1+','+date+','+time_id+')">确认完成</button>';
                        }
                    }else if(date>1){
                    	          button='<button onclick="sub('+order_id+','+1+','+date+','+time_id+')">确认完成</button>';
                    }

                    var timee =this.order_time.substring(5,10);

                    if(this.order_type=='test'){
                        this.order_type='约教'
                    }else{
                        this.order_type='送考'
                    }
                    if(this.subject=='two'){
                        this.subject='二'
                    }else{
                        this.subject='三'
                    }
                    $("#test_order").append("<div class='orders orders2' id='test_order'><div class='oder_canceled'><p>教学订单</p></div><div class='cancle_oders_list'><div class='cancle_reason clearfix'><ul><li class='appoint_time float_li1'><ul><li>"+timee+"</li></ul></li><li class='head_portrait float_li1'><img src="+this.photo+" alt=''><p>"+this.user_name+"</p></li><li class='subject_reason float_li1'><h3>科"+this.subject+this.order_type+"</h3><span>"+this.time_name+"</span><p>"+this.meet_address+"</p></li><li class='right_arrow float_li1'><img src='img/right_arrow.png' alt='向右箭头'></li></ul></div></div><div class='btn_list clearfix'><button onclick='go("+this.phone+")'>电话联系</button>"+button+"</div></div>");

                });
                $(data.conduct['shuttle']).each(function(){
                    var timee =this.order_time.substring(5,10);
                    if(this.order_type=='test'){
                        this.order_type='约教'
                    }else{
                        this.order_type='送考'
                    }
                    if(this.subject=='two'){
                        this.subject='二'
                    }else{
                        this.subject='三'
                    }
                    $("#shuttle").append("<div class='orders orders2' id='shuttle'> <div class='oder_canceled'><p>送考订单</p></div><div class='cancle_oders_list'><div class='cancle_reason clearfix'><ul><li class='appoint_time float_li1'><ul><li>"+timee+"</li></ul></li><li class='head_portrait float_li1'><img src="+this.photo+" alt=''><p>"+this.user_name+"</p></li><li class='subject_reason float_li1'><h3>科"+this.subject+this.order_type+"</h3><span>"+this.time_name+"</span><p>"+this.meet_address+"</p></li><li class='right_arrow float_li1'><img src='img/right_arrow.png' alt='向右箭头'></li></ul></div></div><div class='btn_list clearfix'><button onclick='go("+this.phone+")'>电话联系</button><button>确认取消</button></div></div>");

                });
                $(data.clear['test']).each(function(){
                    var timee =this.order_time.substring(5,10);
                    var order_ID=this.order_id;
                    if(this.order_type=='test'){
                        this.order_type='约教'
                    }else{
                        this.order_type='送考'
                    }
                    if(this.subject=='two'){
                        this.subject='二'
                    }else{
                        this.subject='三'
                    }
                    $("#can").append("<div class='orders  orders1' id='can'><div class='oder_canceled'></div><div class='cancle_oders_list'><div class='cancle_reason clearfix'><ul><li class='appoint_time float_li1'><ul><li>"+timee+"</li></ul></li><li class='head_portrait float_li1'><img src="+this.photo+" alt=''><p>"+this.user_name+"</p></li><li class='subject_reason float_li1'><h3>科"+this.subject+this.order_type+"</h3><span>"+this.time_name+"</span><p>"+this.cancle+"</p></li><li class='right_arrow float_li1'><img src='img/right_arrow.png' alt='向右箭头'></li></ul></div></div><div class='btn_list clearfix'><button onclick='go("+this.phone+")'>电话联系</button><button onclick='report("+order_ID+")'>举报</button></div></div>");
                });
                $(data.clear['shuttle']).each(function(){
                    var timee =this.order_time.substring(5,10)
                    var order_ID=this.order_id;
                    if(this.order_type=='test'){
                        this.order_type='约教'
                    }else{
                        this.order_type='送考'
                    }
                    if(this.subject=='two'){
                        this.subject='二'
                    }else{
                        this.subject='三'
                    }
                    $("#can").append("<div class='orders  orders1' id='can'><div class='oder_canceled'><p>学员取消的订单</p></div><div class='cancle_oders_list'><div class='cancle_reason clearfix'><ul><li class='appoint_time float_li1'><ul><li>"+time+"</li></ul></li><li class='head_portrait float_li1'><img src="+this.photo+" alt=''><p>"+this.user_name+"</p></li><li class='subject_reason float_li1'><h3>科"+this.subject+this.order_type+"</h3><span>"+this.time_name+"</span><p>"+this.cancle+"</p></li><li class='right_arrow float_li1'><img src='img/right_arrow.png' alt='向右箭头'></li></ul></div></div><div class='btn_list clearfix'><button onclick='go("+this.phone+")'>电话联系</button><button onclick='report("+order_ID+")'>举报</button></div></div>");
                });
                $(data.complete).each(function(){
                    var timee =this.order_time.substring(5,10)
                    if(this.order_type=='test'){
                        this.order_type='约教'
                    }else{
                        this.order_type='送考'
                    }
                    if(this.subject=='two'){
                        this.subject='二'
                    }else{
                        this.subject='三'
                    }
                    $(".orders3").append("<div class='cancle_oders_list' id='complete'><div class='cancle_reason clearfix'><ul><li class='appoint_time float_li1'><ul><li>"+timee+"</li></ul></li><li class='head_portrait float_li1'><img src="+this.photo+" alt=''><p>"+this.user_name+"</p></li><li class='subject_reason float_li1'><h3>科"+this.subject+this.order_type+"</h3><span>"+this.time_name+"</span><p>"+this.assess+"</p></li><li class='right_arrow float_li1'><img src='../public_img/right_arrow.png' alt='向右箭头'></li></ul></div></div>");
                });

            }
            if(data.status==250){
                layer.msg("请重新登录");
                setInterval(function(){
                    window.location.href="../login/log_in.html";
                },2000);
            }
        },
        error:function(){
            layer.msg('系统繁忙',function(){
                history.go(0)
            },1000);
        }
    });

});
function time(t){
    var arr=[];
    t=parseInt(t);
    switch (t)
    {
        case 1:
            arr.push(6.5,7.25);
            break;
        case 2:
            arr.push(7+5/6,9+35/60);
            break;
        case 3:
            arr.push(9+1/6,10+55/60);
            break;
        case 4:
            arr.push(10.75,12.25);
            break;
        case 5:
            arr.push(11+5/6,13+35/60);
            break;
        case 6:
            arr.push(13+1/6,14+55/60);
            break;
        case 7:
            arr.push(14.5,16.25);
            break;
        case 8:
            arr.push(15+5/6,17+35/60);
            break;
        case 9:
            arr.push(17+1/6,18+55/60);
            break;
    }
    return arr;
}

function sub(order_id,type,date,time_id){
    var c=time(time_id);
    if(date>c[1]){
        location.reload();
    }
    var token=localStorage.phone;
    $.ajax({
        url:'https://newapp.icandrive.cn/api/updateorder.php',
        type:'post',
        dataType:'json',
        data:{order_id:order_id,type:type,token:token},
        success: function (data) {
            console.log(data);
            if(data.status==200){

                history.go(0);
            }
            else{
                alert(1)

            }

        },
        error:function (){
            layer.msg('系统繁忙',function(){
                history.go(0)
            },1000);
        }
    })




}
function cancel(order_id,type,date,time_id){
    var b=time(time_id);
    if(date>b[1]){
        location.reload();
    }
    localStorage.order_id=order_id;
    localStorage.type=type;
    window.location.href='../report/cancel_reason.html';

}
function report(order_id){

                localStorage.order_id=order_id;
                window.location.href='../report/teacher_report.html';



}
function go(phone){
    window.location.href='tel://'+phone;
}
